<template>
  <div v-show="$route.name == 'process-back'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>肉码出入库回滚</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card style="margin-bottom: 15px;">
      <div style="margin-bottom: 20px;">肉码出入库回滚</div>
      <!-- 搜索与添加区域 -->
      <el-row class="query">
        <el-col :span="6">
          <el-date-picker
            v-model="backTime"
            type="date"
            :clearable="false"
            value-format='yyyy-MM-dd'
            :picker-options="pickerOptions"
            placeholder="选择日期">
          </el-date-picker>
        </el-col>
      </el-row>
      <el-row class="query">
        <el-button type="danger" @click="actBack()" :loading="loading" style="margin-right: 5px;">操作回滚</el-button>
      </el-row>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      backTime: '',
      nowDate: '',
      pickerOptions: {
        disabledDate: time => {
          // 7天的时间戳
          const one = 8 * 24 * 3600 * 1000
          // // 当前日期 - one = 7天之前
          const minTime = this.nowDate - one
          // // 当前日期 + one = 7天之后
          const maxTime = this.nowDate
          if (time.getTime() < minTime || time.getTime() > maxTime) {
            return true
          } else {
            return false
          }
        }
      }
    }
  },
  mounted() {
    const myDate = new Date();
    const year = myDate.getFullYear() // 获取当前年份
    const month = myDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
    const day = myDate.getDate() // 获取当前日（1-31）
    this.backTime = `${year}-${month}-${day}`
    // 日期格式：2019/07/29 - 2019/07/29
    this.nowDate = Date.now()
  },
  methods: {
    async actBack() {
      const myDate = new Date(this.backTime)
      const year = myDate.getFullYear() // 获取当前年份
      const month = myDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
      const day = myDate.getDate() // 获取当前日（1-31）
      // 日期格式：2019/07/29 - 2019/07/29
      // const { data: result } = await this.$http.get('/process/detail', { params: {processTime:  `${year}-${month}-${day}`} })
      var msg = '即将回滚' + `${year}-${month}-${day}` + '到至今数据，是否继续？'
      const confirmResult = await this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: result } = await this.$http.delete('/process/delete', { params: {processTime:  `${year}-${month}-${day}`} })
      if (result.status !== 200) return this.$message.error(result.msg)
      this.$message.success('删除成功')
    }
  }
}
</script>
<style scoped>
  .query {
    margin-bottom: 10px;
  }
</style>